<template>
    <view>
        <div style="margin-bottom: 60px;">
            <u-cell-item v-for="(item, index) in tbShippingAddressList" :key="item.id" :arrow="false" use-label-slot>
                <view slot="title" style="display: flex; font-weight: bold; font-size: 16px;">
                    <div style="margin-right: 25px;">
                        {{ item.consignee }}
                    </div>
                    <div style="margin-right: 25px;">
                        {{ item.phone }}
                    </div>
                    <div>
                        <u-tag v-if="item.isDefault" text="默认" type="error" shape="circle" mode="dark"/>
                    </div>
                </view>
                <view slot="label">
                    {{item.provinceName + item.cityName + item.areaName + item.address}}
                </view>
                <u-icon slot="right-icon" size="40" name="edit-pen" @click="action(item, index)" style="padding-left: 20px;"></u-icon>
            </u-cell-item>
        </div>

        <div v-if="!tbShippingAddressList.length">
            <u-empty text="没有添加收货地址" mode="address"></u-empty>
        </div>

        <div class="custom-bottom-button"
             @tap="navigateTo('/pages/sub/sub1-tabs/tabs/person/shipping-address/shipping-address-add/index', {isDefault: tbShippingAddressList.length === 0})">
            <label>新建收货地址</label>
        </div>
    </view>
</template>

<script>
    import {TbShippingAddressService} from "@/dev/services/tb-shipping-address.service";
    import {CommonUtil} from "@/dev/core/utils/common.util";

    export default {
        data() {
            return {
                tbShippingAddressList: []
            };
        },
        onShow() {
            this.findTbShippingAddressList()
        },
        methods: {
            findTbShippingAddressList() {
                new TbShippingAddressService().findTbShippingAddressList({
                    communityId: this.globalVariable.userInfo.tbCommunity.id,
                    userId: this.globalVariable.userInfo.id,
                    orderBy: 'is_default desc, create_time desc'
                }).then(result => {
                    this.tbShippingAddressList = result
                })
            },
            action(item, index) {
                const _self = this;
                uni.showActionSheet({
                    itemList: ['设为默认', '编辑', '删除'],
                    success: function (res) {
                        console.log(res)
                        if (res.tapIndex === 0) {
                            _self.isDefaultItem(item);
                        } else if (res.tapIndex === 1) {
                            _self.navigateTo('/pages/sub/sub1-tabs/tabs/person/shipping-address/shipping-address-edit/index', item);
                        } else if (res.tapIndex === 2) {
                            _self.deleteItem(item, index);
                        }
                    }
                });
            },
            deleteItem(item, index) {
                const _self = this
                CommonUtil.alert({
                    title: '提示',
                    content: '确认删除吗？',
                    showCancel: true, // 是否显示取消按钮
                    cancelText: '取消', // 默认是“取消”
                    confirmText: '确定', // 默认是“确定”
                    success: function (res) {
                        if (res.cancel) {
                            // 点击取消,默认隐藏弹框
                        } else {
                            // 点击确定
                            new TbShippingAddressService().batchDeleteTbShippingAddressByIds(item.id).then(result => {
                                _self.tbShippingAddressList.splice(index, 1);
                                CommonUtil.toast('删除成功')
                            })
                        }
                    }
                });
            },
            /**
             * 设置成默认地址
             * @param item
             */
            isDefaultItem(item) {
                new TbShippingAddressService().setDefaultTbShippingAddress(item).then(result => {
                    CommonUtil.toast('设为默认成功').then(() => {
                        this.findTbShippingAddressList()
                    })
                })
            }
        }
    };
</script>

<style lang="scss" scoped>
</style>
